﻿using System;
using ETModel;
using UnityEngine;

namespace ETHotfix {
    [ObjectSystem]
    public class OperaComponentAwakeSystem : AwakeSystem<OperaComponent> {
        public override void Awake(OperaComponent self) {
            self.Awake();
        }
    }

    [ObjectSystem]
    public class OperaComponentUpdateSystem : UpdateSystem<OperaComponent> {
        public override void Update(OperaComponent self) {
            self.Update();
        }
    }

    public class OperaComponent : Component {

        private Unit _operaUnit;
        private AnimatorComponent _opearAni;
        private TurnComponent _turnComp;

        public Vector3 ClickPoint;
        public Unit OperaUnit {
            get {
                return _operaUnit;
            }
            set {
                _operaUnit = value;
                _opearAni = _operaUnit.GetComponent<AnimatorComponent>();
                _turnComp= _operaUnit.GetComponent<TurnComponent>();
            }
        }

        public int mapMask;

        public void Awake() {
            this.mapMask = LayerMask.GetMask("Map");
        }

        private readonly Frame_ClickMap frameClickMap = new Frame_ClickMap();
        private readonly Frame_ShootBox frameShootBox = new Frame_ShootBox();

        public void Update() {
            if (OperaUnit == null || OperaUnit.IsDisposed || _opearAni == null) return;

            if (Input.GetMouseButtonDown(1) && !_opearAni.Animator.GetCurrentAnimatorStateInfo(0).IsName("Attack")) {
                //右键点地面移动, 攻击状态下不能移动
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                var maxDis = 10000;
                if (Physics.Raycast(ray, out hit, maxDis, this.mapMask)) {
                    this.ClickPoint = hit.point;
                    frameClickMap.X = this.ClickPoint.x;
                    frameClickMap.Y = this.ClickPoint.y;
                    frameClickMap.Z = this.ClickPoint.z;
                    SessionComponent.Instance.Session.Send(frameClickMap);
                }
            }

            if (Input.GetKeyDown(KeyCode.Q)) {
                _opearAni.SetTrigger("Jab");
            }
            if (Input.GetKeyDown(KeyCode.E)) {
                _opearAni.SetTrigger("Spinkick");
            }
            if (Input.GetKeyDown(KeyCode.W)) {
                _opearAni.SetTrigger("Hikick");
            }
            if (Input.GetKeyDown(KeyCode.R)) {
                _opearAni.SetTrigger("ScrewK");
            }
            
            if (Input.GetKeyDown(KeyCode.D)) {
                _opearAni.SetTrigger("DamageDown");
                
            }

            if (Input.GetKeyDown(KeyCode.Space)) {
                //往指针头方向发一个方块
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                frameShootBox.X = ray.direction.x;
                frameShootBox.Y = ray.direction.y;
                frameShootBox.Z = ray.direction.z;
                SessionComponent.Instance.Session.Send(frameShootBox);
            }
        }

    }
}
